#
# Copyright (C) 2018 Jianhui Zhao
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=rtty
PKG_VERSION:=7.2.1
PKG_RELEASE:=1

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL=https://github.com/zhaojh329/rtty/releases/download/v$(PKG_VERSION)
PKG_HASH:=10675a12f4333d5b552b6489f04ca4be8821bb67c1fcfca7b002ed7a7fe498c7
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(BUILD_VARIANT)/$(PKG_NAME)-$(PKG_VERSION)
CMAKE_INSTALL:=1

PKG_LICENSE:=MIT
PKG_LICENSE_FILES:=LICENSE

PKG_MAINTAINER:=Jianhui Zhao <zhaojh329@gmail.com>

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk

define Package/rtty7/Default
  TITLE:=Access your terminals from anywhere via the web
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=Terminal
  URL:=https://github.com/zhaojh329/rtty
  DEPENDS:=+libev $(2)
  VARIANT:=$(1)
  PROVIDES:=rtty
endef

Package/rtty7-openssl=$(call Package/rtty7/Default,openssl,+PACKAGE_rtty7-openssl:libopenssl)
Package/rtty7-wolfssl=$(call Package/rtty7/Default,wolfssl,+PACKAGE_rtty7-wolfssl:libwolfssl)
Package/rtty7-mbedtls=$(call Package/rtty7/Default,mbedtls,+PACKAGE_rtty7-mbedtls:libmbedtls +PACKAGE_rtty7-mbedtls:zlib)
Package/rtty7-nossl=$(call Package/rtty7/Default,nossl)

define Package/rtty7-openssl/conffiles
/etc/config/rtty
endef

Package/rtty7-wolfssl/conffiles = $(Package/rtty7-openssl/conffiles)
Package/rtty7-mbedtls/conffiles = $(Package/rtty7-openssl/conffiles)
Package/rtty7-nossl/conffiles = $(Package/rtty7-openssl/conffiles)

ifeq ($(BUILD_VARIANT),openssl)
  CMAKE_OPTIONS += -DRTTY_USE_OPENSSL=ON
else ifeq ($(BUILD_VARIANT),wolfssl)
  CMAKE_OPTIONS += -DRTTY_USE_WOLFSSL=ON
else ifeq ($(BUILD_VARIANT),mbedtls)
  CMAKE_OPTIONS += -DRTTY_USE_MBEDTLS=ON
else
  CMAKE_OPTIONS += -DRTTY_SSL_SUPPORT=OFF
endif

define Package/rtty7-$(BUILD_VARIANT)/install
	$(INSTALL_DIR) $(1)/usr/sbin $(1)/etc/init.d $(1)/etc/config
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/rtty $(1)/usr/sbin
	$(INSTALL_BIN) ./files/rtty.init $(1)/etc/init.d/rtty
	$(INSTALL_CONF) ./files/rtty.config $(1)/etc/config/rtty
endef

$(eval $(call BuildPackage,rtty7-openssl))
$(eval $(call BuildPackage,rtty7-wolfssl))
$(eval $(call BuildPackage,rtty7-mbedtls))
$(eval $(call BuildPackage,rtty7-nossl))
